﻿using Autofac.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;

namespace SuperJoe.Framework.Ioc
{
    public static class AutofacIocContainerExtension
    {
        public static IIocContainer RegisterType<TService, TImplemetation>(this IIocContainer container, Action<IActivatingEventArgs<TImplemetation>> activatingAction, string name = null) where TImplemetation : TService
        {
            AutofacIocContainer autofacIocContainer = container as AutofacIocContainer;

            AutofacIocContainer.UpdateContainer((builder) =>
            {
                var register = builder.RegisterType<TImplemetation>().As<TService>();

                if (name != null)
                {
                    register = register.Named<TService>(name);
                }

                register = register.OnActivating(activatingAction);
            });

            return autofacIocContainer;
        }

        public static IIocContainer RegisterTypePerLifetimeScope<TService, TImplemetation>(this IIocContainer container, string name = null) where TImplemetation : TService
        {
            AutofacIocContainer autofacIocContainer = container as AutofacIocContainer;
            AutofacIocContainer.UpdateContainer((builder) =>
            {
                var register = builder.RegisterType<TImplemetation>().As<TService>().InstancePerLifetimeScope();

                if (name != null)
                {
                    register = register.Named<TService>(name);
                }
            });

            return autofacIocContainer;
        }
        public static IIocContainer RegisterTypePerLifetimeScope<TService, TImplemetation>(this IIocContainer container, Action<IActivatingEventArgs<TImplemetation>> activatingAction, string name = null) where TImplemetation : TService
        {
            AutofacIocContainer autofacIocContainer = container as AutofacIocContainer;
            AutofacIocContainer.UpdateContainer((builder) =>
            {
                var register = builder.RegisterType<TImplemetation>().As<TService>().InstancePerLifetimeScope();

                if (name != null)
                {
                    register = register.Named<TService>(name);
                }

                register = register.OnActivating(activatingAction);
            });

            return autofacIocContainer;
        }

        public static IIocContainer RegisterTypePerLifetimeScope<TImplemetation>(this IIocContainer container, string name = null)
        {
            AutofacIocContainer autofacIocContainer = container as AutofacIocContainer;
            AutofacIocContainer.UpdateContainer((builder) =>
            {
                var register = builder.RegisterType<TImplemetation>().AsImplementedInterfaces().AsSelf().InstancePerLifetimeScope();
                if (name != null)
                {
                    register = register.Named<TImplemetation>(name);
                }
            });

            return autofacIocContainer;
        }

        public static IIocContainer RegisterTypePerLifetimeScope<TImplemetation>(this IIocContainer container, Action<IActivatingEventArgs<TImplemetation>> activatingAction, string name = null)
        {
            AutofacIocContainer autofacIocContainer = container as AutofacIocContainer;
            AutofacIocContainer.UpdateContainer((builder) =>
            {
                var register = builder.RegisterType<TImplemetation>().AsImplementedInterfaces().AsSelf().InstancePerLifetimeScope();
                if (name != null)
                {
                    register = register.Named<TImplemetation>(name);
                }

                register = register.OnActivating(activatingAction);
            });

            return autofacIocContainer;
        }
    }
}
